﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework;
using DataAccess;

namespace DomMgmtSystem.View
{
    public partial class InCheck : System.Web.UI.Page
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private decimal NID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["NID"]))
                    return Convert.ToDecimal(Request["NID"]);
                return 0;
            }
        }

        /// <summary>
        /// 是否是添加
        /// </summary>
        public bool IsNew
        {
            get
            {
                if (ViewState["IsNew"] != null)
                    return Convert.ToBoolean(ViewState["IsNew"]);
                else
                    return this.GetRequestStr("isnew") != string.Empty;
            }

            set
            {
                ViewState["IsNew"] = value;
            }
        }

        /// <summary>
        /// 是否是浏览
        /// </summary>
        public bool IsView
        {
            get
            {
                return this.GetRequestStr("view") != string.Empty;
            }

        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBind();

            if (Request.Params["view"] != null)
                ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();</script>");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataAccess.CustomerDAC.CustomerDAC dac = new DataAccess.CustomerDAC.CustomerDAC();
                if (dac.IsExist(Convert.ToDecimal(txtSQDH.Text.Trim())) && NID == 0)
                {
                    this.Alert("摄区代号已存在，请重新输入摄区代号");
                    return;
                }
                Model.TB_CHECK model = new Model.TB_CHECK();
                if (NID != 0)
                    model = DataAccess.TB_CHECK.GetModel(NID);

                model.SQDH = Convert.ToDecimal(txtSQDH.Text.Trim());
                model.CSQMC = txtCSQMC.Text.Trim();
                model.WZXJC = rbWZXPass.Checked ? 2 : 1;
                model.YZXJC = rbYZXPass.Checked ? 2 : 1;
                model.ZTJL = rbZTJLPass.Checked ? 2 : 1;
                model.YPSFKD = rbReadable.Checked ? 2 : 1;
                model.CREATE_DATE = DateTime.Now;
                model.CREATE_USER = Session["userID"].ToString();
                model.STATE = 0;

                if (NID != 0)
                {
                    model.NID = NID;
                    if (TB_CHECK.Update(model))
                        this.RedirectAfterInfo("修改成功！", "InCheckList.aspx");
                    else
                        this.Alert("修改失败！");
                }
                else
                    if (TB_CHECK.Add(model))
                    { this.RedirectConfirmInfo("新增成功！是否继续新增？", "InCheck.aspx", "InCheckList.aspx"); }
                    else
                    { this.Alert("保存失败！"); }

            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("入库检查保存异常:{0}{1}", ex.Message, ex.StackTrace);
                this.Alert("保存异常！");
            }
        }

        public void DataBind()
        {
            if (NID != 0)
            {
                Model.TB_CHECK model = DataAccess.TB_CHECK.GetModel(NID);
                if (model != null && model.STATE == 0)
                {
                    txtSQDH.Text = model.SQDH.ToString();
                    txtCSQMC.Text = model.CSQMC;
                    if (model.WZXJC == 2)
                    {
                        rbWZXPass.Checked = true;
                        rbWZXUnPass.Checked = false;
                    }
                    else
                    {
                        rbWZXPass.Checked = false;
                        rbWZXUnPass.Checked = true;
                    }

                    if (model.YZXJC == 2)
                    {
                        rbYZXPass.Checked = true;
                        rbYZXUnPass.Checked = false;
                    }
                    else
                    {
                        rbYZXPass.Checked = false;
                        rbYZXUnPass.Checked = true;
                    }

                    if (model.ZTJL == 2)
                    {
                        rbZTJLPass.Checked = true;
                        rbZTJLUnPass.Checked = false;
                    }
                    else
                    {
                        rbZTJLPass.Checked = false;
                        rbZTJLUnPass.Checked = true;
                    }

                    if (model.YPSFKD == 2)
                    {
                        rbReadable.Checked = true;
                        rbUnReadable.Checked = false;
                    }
                    else
                    {
                        rbReadable.Checked = false;
                        rbUnReadable.Checked = true;
                    }
                }
                btnBack.Visible = true;
                if (Request.Params["view"] != null)
                    lbTitle.Text = "查看入库检查";
                else
                    lbTitle.Text = "编辑入库检查";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("InCheckList.aspx");
        }
    }
}